﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine;
using PloobsEngine.Cameras;
using PloobsEngine.Input;
using Microsoft.Xna.Framework;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Utils;
using PloobsEngineDemo3;
using PloobsEngine.Features;
using PloobsEngine.Commands;
using PloobsEngine.Loader;
namespace Etapa1.Screens
{
    public class LoaderScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;

        public LoaderScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);                        
            engine.Game.IsFixedTimeStep = true;
        }
        
        public override void LoadContent(EngineStuff es)
        {            
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            base.LoadContent(es);            

            #region LOADER

            ExtractXmlModelLoader me = new ExtractXmlModelLoader();
            ModelLoaderData data = me.Load("kitchen");

            foreach (var item in data.ModelMeshesInfo)
            {
                ComposedPartsModel sm = new ComposedPartsModel(item);
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(sm, item.pos, Matrix.CreateFromQuaternion(item.ori), item.scale);
                pi.isMotionLess = true;
                CustomDeferred shader = new CustomDeferred(item.HasTexture(TextureType.GLOW), item.HasTexture(TextureType.BUMP), item.HasTexture(TextureType.SPECULAR), item.HasTexture(TextureType.PARALAX));
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
            }
            
            foreach (var item in data.LightsInfo)
            {
                   mundo.AddLight(item);             
            }

            foreach (var item in data.CameraInfo)
            {                
                CameraStatic sc = new CameraStatic(item.Position, item.Target);
                sc.Name = item.Name;
                mundo.AddCamera(sc.Name,sc);
            }

            #region NormalLight
            //DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            //DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            //DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            //DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            //DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            //float li = 0.3f;
            //ld1.LightIntensity = li;
            //ld2.LightIntensity = li;
            //ld3.LightIntensity = li;
            //ld4.LightIntensity = li;
            //ld5.LightIntensity = li;
            //mundo.AddLight(ld1);
            //mundo.AddLight(ld2);
            //mundo.AddLight(ld3);
            //mundo.AddLight(ld4);
            //mundo.AddLight(ld5);
            #endregion

            #endregion            

            cam = new CameraFirstPerson(true);
            cam.FarPlane = 1000;
            cam.Name = "Fp";            
            mundo.AddCamera(cam.Name, cam);

            //mundo.CameraManager.SetActiveCamera("Camera01");
            mundo.CameraManager.SetActiveCamera("Fp");

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);                                

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);
            //dr.isDebug = true;
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            dr.DeferredFinalCombination.AmbientColor = new Color(0.1f, 0.1f, 0.1f);
            ShadowLightMap shadow = new ShadowLightMap();            
            shadow.ShadownBufferSize = 1024;            
            dr.DeferredLightMap = shadow;

            dr.AddPostEffect(new AntiAliasingPostEffect());

            this.RenderTechnics = rt;                        
            this.World = mundo;
        }



    }
}

